/**
 * @file testlog.cpp
 * @brief
 * @author Wang Bo
 * @version 1.0
 * @date 2021-03-22
 *
 * @copyright co.,Ltd.
 *
 * @par 修改日志:
 * 	Date	Version	Author
 * 	2021-03-22	1.0	Wang Bo
 * 	内容：
 * 	测试日志类
 */

#include "logger.h"
#include <iostream>
#include <random>
#include <thread>
#include <vector>

using namespace wb;

int main(int argc, const char** argv) {
    Logger::setLogLevel(LogLevel::TRACE);
    LOG(LogLevel::TRACE) << "level trace";
    LOG(LogLevel::INFO) << "level info";
    LOG(LogLevel::DEBUG) << "level debug";
    LOG(LogLevel::WARN) << "level warn";
    LOG(LogLevel::ERROR) << "level errro";
    LOG(LogLevel::FATAL) << "level fatal";

    std::cout << "==============set to info=================" << std::endl;

    Logger::setLogLevel(LogLevel::INFO);
    LOG(LogLevel::TRACE) << "level trace";
    LOG(LogLevel::INFO) << "level info";
    LOG(LogLevel::DEBUG) << "level debug";
    LOG(LogLevel::WARN) << "level warn";
    LOG(LogLevel::ERROR) << "level errro";
    LOG(LogLevel::FATAL) << "level fatal";

    std::cout << "==============set to debug=================" << std::endl;

    Logger::setLogLevel(LogLevel::DEBUG);
    LOG(LogLevel::TRACE) << "level trace";
    LOG(LogLevel::INFO) << "level info";
    LOG(LogLevel::DEBUG) << "level debug";
    LOG(LogLevel::WARN) << "level warn";
    LOG(LogLevel::ERROR) << "level errro";
    LOG(LogLevel::FATAL) << "level fatal";

    std::cout << "==============set to warn=================" << std::endl;

    Logger::setLogLevel(LogLevel::WARN);
    LOG(LogLevel::TRACE) << "level trace";
    LOG(LogLevel::INFO) << "level info";
    LOG(LogLevel::DEBUG) << "level debug";
    LOG(LogLevel::WARN) << "level warn";
    LOG(LogLevel::ERROR) << "level errro";
    LOG(LogLevel::FATAL) << "level fatal";

    std::cout << "==============set to error=================" << std::endl;

    Logger::setLogLevel(LogLevel::ERROR);
    LOG(LogLevel::TRACE) << "level trace";
    LOG(LogLevel::INFO) << "level info";
    LOG(LogLevel::DEBUG) << "level debug";
    LOG(LogLevel::WARN) << "level warn";
    LOG(LogLevel::ERROR) << "level errro";
    LOG(LogLevel::FATAL) << "level fatal";

    //简易线程水塘测试并发日志输出 (10线程*100次循环)
    Logger::setLogLevel(LogLevel::TRACE); //重置日志级别
    #include <ctime>
    using namespace std;
    vector<thread> pool;
    default_random_engine e(time(0));
    uniform_int_distribution<int> d(0, 5);
    for (int i = 0; i < 10; ++i) {
        pool.emplace_back(thread(
            [&](){
                thread::id tid = this_thread::get_id(); //获取当前线程号
                for (int i = 0; i < 100; ++i) {
                    LogLevel lvl = static_cast<LogLevel>(d(e)); //每次输出日志前随机一个级别
                    LOG(lvl) << tid << ": do log for " << i << " times";
                }
            }
        ));
    }
    for (auto &t : pool) {
        t.join();
    }
    return 0;
}